package com.floreantpos.ui.views;

import com.floreantpos.main.Application;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.dialog.NotesDialog;
import com.floreantpos.ui.dialog.NumberSelectionDialog2;
import com.floreantpos.ui.dialog.POSMessageDialog;
import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GuestView extends TransparentPanel {

  private DecimalFormat decimalFormat = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(new Locale("pt", "BR")));
  private Double deliveryCharge = Double.valueOf(0);

  /**
   * Creates new form GuestView
   */
  public GuestView(String guestPhone) {
    initComponents(guestPhone);
  }

  public void initialize() {

  }

  private void initComponents(String guestPhone) {
    telephoneSelectionView = new com.floreantpos.ui.views.TelephoneSelectionView();

    jLabel1 = new javax.swing.JLabel();
    btnAddName = new com.floreantpos.swing.PosButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    tfName = new javax.swing.JTextArea();

    jLabel2 = new javax.swing.JLabel();
    btnAddAddressLine1 = new com.floreantpos.swing.PosButton();
    jScrollPane2 = new javax.swing.JScrollPane();
    tfAddressLine1 = new javax.swing.JTextArea();

    jLabel3 = new javax.swing.JLabel();
    btnAddAddressLine2 = new com.floreantpos.swing.PosButton();
    jScrollPane3 = new javax.swing.JScrollPane();
    tfAddressLine2 = new javax.swing.JTextArea();

    jLabel4 = new javax.swing.JLabel();
    btnAddAddressLine3 = new com.floreantpos.swing.PosButton();
    jScrollPane4 = new javax.swing.JScrollPane();
    tfAddressLine3 = new javax.swing.JTextArea();

    jLabel5 = new javax.swing.JLabel();
    btnDeliveryCharge = new com.floreantpos.swing.PosButton();
    jScrollPane5 = new javax.swing.JScrollPane();
    tfDeliveryCharge = new javax.swing.JTextArea();

    telephoneSelectionView.setTitle(com.floreantpos.POSConstants.TELEPHONE);
    telephoneSelectionView.setValue(guestPhone);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel1.setText(com.floreantpos.POSConstants.FIRST_NAME);

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel2.setText(com.floreantpos.POSConstants.ADDRESS_LINE1);

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel3.setText(com.floreantpos.POSConstants.ADDRESS_LINE2);

    jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel4.setText(com.floreantpos.POSConstants.ADDRESS_LINE3);

    jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel5.setText(com.floreantpos.POSConstants.DELIVERY_RATE);

    btnAddName.setText("...");
    btnAddName.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnAddNameActionPerformed(evt);
      }
    });

    btnAddAddressLine1.setText("...");
    btnAddAddressLine1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnAddAddressLine1ActionPerformed(evt);
      }
    });

    btnAddAddressLine2.setText("...");
    btnAddAddressLine2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnAddAddressLine2ActionPerformed(evt);
      }
    });

    btnAddAddressLine3.setText("...");
    btnAddAddressLine3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnAddAddressLine3ActionPerformed(evt);
      }
    });

    btnDeliveryCharge.setText("...");
    btnDeliveryCharge.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnDeliveryChargeActionPerformed(evt);
      }
    });

    tfName.setColumns(20);
    tfName.setEditable(false);
    tfName.setLineWrap(true);
    tfName.setRows(5);
    tfName.setWrapStyleWord(true);
    tfName.setBackground(Color.WHITE);
    jScrollPane1.setViewportView(tfName);

    tfAddressLine1.setColumns(20);
    tfAddressLine1.setEditable(false);
    tfAddressLine1.setLineWrap(true);
    tfAddressLine1.setRows(5);
    tfAddressLine1.setWrapStyleWord(true);
    tfAddressLine1.setBackground(Color.WHITE);
    jScrollPane2.setViewportView(tfAddressLine1);

    tfAddressLine2.setColumns(20);
    tfAddressLine2.setEditable(false);
    tfAddressLine2.setLineWrap(true);
    tfAddressLine2.setRows(5);
    tfAddressLine2.setWrapStyleWord(true);
    tfAddressLine2.setBackground(Color.WHITE);
    jScrollPane3.setViewportView(tfAddressLine2);

    tfAddressLine3.setColumns(20);
    tfAddressLine3.setEditable(false);
    tfAddressLine3.setLineWrap(true);
    tfAddressLine3.setRows(5);
    tfAddressLine3.setWrapStyleWord(true);
    tfAddressLine3.setBackground(Color.WHITE);
    jScrollPane4.setViewportView(tfAddressLine3);

    tfDeliveryCharge.setColumns(20);
    tfDeliveryCharge.setEditable(false);
    tfDeliveryCharge.setLineWrap(true);
    tfDeliveryCharge.setRows(5);
    tfDeliveryCharge.setWrapStyleWord(true);
    tfDeliveryCharge.setBackground(Color.WHITE);
    jScrollPane5.setViewportView(tfDeliveryCharge);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(telephoneSelectionView, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(btnAddName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(btnAddAddressLine1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(btnAddAddressLine2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(btnAddAddressLine3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(jScrollPane5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(btnDeliveryCharge, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
    );

    layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(telephoneSelectionView, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 347, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(layout.createSequentialGroup()
                                                    .add(jLabel1)
                                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
                                            .add(btnAddName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(layout.createSequentialGroup()
                                                    .add(jLabel2)
                                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
                                            .add(btnAddAddressLine1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(layout.createSequentialGroup()
                                                    .add(jLabel3)
                                                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
                                            .add(btnAddAddressLine2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(layout.createSequentialGroup()
                                                    .add(jLabel4)
                                                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
                                            .add(btnAddAddressLine3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(layout.createSequentialGroup()
                                                    .add(jLabel5)
                                                    .add(jScrollPane5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
                                            .add(btnDeliveryCharge, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap())
    );
  }

  private void btnAddNameActionPerformed(java.awt.event.ActionEvent evt) {
    NotesDialog dialogName = new NotesDialog(Application.getPosWindow(), true);
    dialogName.setTitle("Nome");
    dialogName.setNote(tfName.getText());
    dialogName.pack();
    dialogName.open();

    if (!dialogName.isCanceled()) {
      tfName.setText(dialogName.getNote());
      if (tfName.getText().isEmpty()) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.NAME_REQUIRED);
        btnAddNameActionPerformed(evt);
      }
    }

    if (dialogName.getNote().length() > 40) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.FIELD_LIMIT);
      tfName.setText(tfName.getText().substring(0, 39));
      btnAddNameActionPerformed(evt);
    }

  }

  private void btnAddAddressLine1ActionPerformed(java.awt.event.ActionEvent evt) {
    NotesDialog dialogAddress1 = new NotesDialog(Application.getPosWindow(), true);
    dialogAddress1.setTitle(com.floreantpos.POSConstants.ADDRESS_LINE1);
    dialogAddress1.setNote(tfAddressLine1.getText());
    dialogAddress1.pack();
    dialogAddress1.open();

    if (!dialogAddress1.isCanceled()) {
      tfAddressLine1.setText(dialogAddress1.getNote());
      if (tfAddressLine1.getText().isEmpty()) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.ADDRESS1_REQUIRED);
        btnAddAddressLine1ActionPerformed(evt);
      }
    }

    if (dialogAddress1.getNote().length() > 40) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.FIELD_LIMIT);
      tfAddressLine1.setText(tfAddressLine1.getText().substring(0, 39));
      btnAddAddressLine1ActionPerformed(evt);
    }

  }

  private void btnAddAddressLine2ActionPerformed(java.awt.event.ActionEvent evt) {
    NotesDialog dialogAddress2 = new NotesDialog(Application.getPosWindow(), true);
    dialogAddress2.setTitle(com.floreantpos.POSConstants.ADDRESS_LINE2);
    dialogAddress2.setNote(tfAddressLine2.getText());
    dialogAddress2.pack();
    dialogAddress2.open();

    if (!dialogAddress2.isCanceled()) {
      tfAddressLine2.setText(dialogAddress2.getNote());
    }

    if (dialogAddress2.getNote().length() > 40) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.FIELD_LIMIT);
      tfAddressLine2.setText(tfAddressLine2.getText().substring(0, 39));
      btnAddAddressLine2ActionPerformed(evt);
    }

  }

  private void btnAddAddressLine3ActionPerformed(java.awt.event.ActionEvent evt) {
    NotesDialog dialogAddress3 = new NotesDialog(Application.getPosWindow(), true);
    dialogAddress3.setTitle(com.floreantpos.POSConstants.ADDRESS_LINE3);
    dialogAddress3.setNote(tfAddressLine3.getText());
    dialogAddress3.pack();
    dialogAddress3.open();

    if (!dialogAddress3.isCanceled()) {
      tfAddressLine3.setText(dialogAddress3.getNote());
    }

    if (dialogAddress3.getNote().length() > 40) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.FIELD_LIMIT);
      tfAddressLine3.setText(tfAddressLine3.getText().substring(0, 39));
      btnAddAddressLine3ActionPerformed(evt);
    }

  }

  private void btnDeliveryChargeActionPerformed(java.awt.event.ActionEvent evt) {

    NumberSelectionDialog2 dialogDeliveryCharge = new NumberSelectionDialog2();
    dialogDeliveryCharge.setTitle(com.floreantpos.POSConstants.DELIVERY_RATE);
    dialogDeliveryCharge.setFloatingPoint(true);

    if (!tfDeliveryCharge.getText().isEmpty()) {
      try {
        dialogDeliveryCharge.setValue(Double.valueOf(decimalFormat.parse(tfDeliveryCharge.getText()).doubleValue()));
      } catch (ParseException ex) {
        Logger.getLogger(GuestView.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    dialogDeliveryCharge.pack();
    dialogDeliveryCharge.open();

    if (!dialogDeliveryCharge.isCanceled()) {
      tfDeliveryCharge.setText(decimalFormat.format(dialogDeliveryCharge.getValue()));
      deliveryCharge = dialogDeliveryCharge.getValue();
    }
  }

  private com.floreantpos.swing.PosButton btnAddName;
  private com.floreantpos.swing.PosButton btnAddAddressLine1;
  private com.floreantpos.swing.PosButton btnAddAddressLine2;
  private com.floreantpos.swing.PosButton btnAddAddressLine3;
  private com.floreantpos.swing.PosButton btnDeliveryCharge;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JScrollPane jScrollPane3;
  private javax.swing.JScrollPane jScrollPane4;
  private javax.swing.JScrollPane jScrollPane5;
  private com.floreantpos.ui.views.TelephoneSelectionView telephoneSelectionView;
  private javax.swing.JTextArea tfName;
  private javax.swing.JTextArea tfAddressLine1;
  private javax.swing.JTextArea tfAddressLine2;
  private javax.swing.JTextArea tfAddressLine3;
  private javax.swing.JTextArea tfDeliveryCharge;

  public String getPhoneNumber() {
    return telephoneSelectionView.getValue();
  }

  public String getName() {
    return tfName.getText();
  }

  public String getAdressLine1() {
    return tfAddressLine1.getText();
  }

  public String getAdressLine2() {
    return tfAddressLine2.getText();
  }

  public String getAdressLine3() {
    return tfAddressLine3.getText();
  }

  public Double getDeliveryCharge() {
    return deliveryCharge;
  }
}
